//
//  FriendListViewController.m
//  PigMarket
//
//  Created by mac on 2019/6/4.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "FriendListViewController.h"
#import "ChatListCell.h"
#import "SearchView.h"
#import "NewFriendViewController.h"
#import "AddFriendViewController.h"
#import "SearchViewController.h"
#import "ChatViewController.h"
@interface FriendListViewController ()<UITableViewDelegate,UITableViewDataSource>
/**
 tableView
 */
@property (nonatomic , strong) UITableView *myTableView;
/**
 dataArr
 */
@property (nonatomic , strong) NSMutableArray *dataArr;
/**
 searchView
 */
@property (nonatomic , strong) SearchView *searchView;
@end

@implementation FriendListViewController



#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"通讯录";
    [self setUpNav];
    [self setUpView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

#pragma mark - init method
- (void)setUpNav {
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"chat_addfriend"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(addFrinedClick)];
}
- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}
- (void)initView {
    [self.view addSubview:self.myTableView];
}

- (void) initLayout {
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (void) initAction {
    
    //通知  同意添加好友后，刷新页面数据
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshFrinedsListNot) name:NotificationAgreeAddFriendChange object:nil];
    
    //修改昵称后  刷新页面
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshFrinedsListNot) name:NotificationFriendNameChange object:nil];
    
    //修改备注后
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshFrinedsListNot) name:NotificationFriendRemarkChange object:nil];

    //搜索
    @weakify(self);
    [self.searchView setSearchBeginBlock:^{
        @strongify(self);
        SearchViewController *vc = [[SearchViewController alloc] init];
        vc.searchType = SearchTypeFriendList;
        [self.navigationController pushViewController:vc animated:YES];
    }];

}

- (void)initData {
    
    @weakify(self);
    [[[PigMarketApi sharedInstance] myFriendsListWithMemberId:[UserInfoDataProvider sharedInstance].userInfo.memberId nickName:@"" currentPage:1] subscribeNext:^(FriendsList * _Nullable friendList) {
        
        @strongify(self);
        NSMutableArray *tempArr = [[NSMutableArray alloc] init];
        //新的豚友和我的群聊
        Friends *newFriendModel = [[Friends alloc] init];
        newFriendModel.headImage = [UIImage imageNamed:@"chat_addfriends"];
        newFriendModel.nickName = @"新的豚友";
        [tempArr addObject:newFriendModel];
        Friends *myGroupModel = [[Friends alloc] init];
        myGroupModel.headImage = [UIImage imageNamed:@"chat_mygroup"];
        myGroupModel.nickName = @"我的群聊";
        [tempArr addObject:myGroupModel];
        
        [self.dataArr addObject:tempArr];
        
        if (![JTool iSNull:friendList.list]) {
            
            [self.dataArr addObject:friendList.list];
        }
        [self.myTableView reloadData];
    } error:^(NSError * _Nullable error) {
        @strongify(self);
        [self handlingError:error];
    }];
}

#pragma mark - delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.dataArr count];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.dataArr.count > 0) {
        return [self.dataArr[section] count];
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 54;
    }
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 54)];
        headerView.backgroundColor = [UIColor globalBackgroundColor];
        self.searchView.frame = CGRectMake(0, 10, SCREEN_WIDTH, 34);
        [headerView addSubview:self.searchView];

        return headerView;
    }
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ChatListCell *cell = [ChatListCell cellWithTableView:tableView];
    Friends *model = self.dataArr[indexPath.section][indexPath.row];
    cell.friendModel = model;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {//新的好友
            [self.navigationController pushViewController:[[NewFriendViewController alloc] init] animated:YES];
            
        } else if (indexPath.row == 1) {
            
        }
    } else {
        Friends *model = self.dataArr[indexPath.section][indexPath.row];
        NSString *title = model.noteName ? model.noteName : model.nickName;
        TUIConversationCellData *data = [[TUIConversationCellData alloc] init];
        data.convId = model.memberNum;
        data.convType = TIM_C2C;
        data.title = title;
        ChatViewController *chat = [[ChatViewController alloc] init];
        chat.conversation = data;
        [self.navigationController pushViewController:chat animated:YES];
    }
}
#pragma mark - target action
- (void)addFrinedClick {
    [self.navigationController pushViewController:[[AddFriendViewController alloc] init] animated:YES];
}
#pragma mark - private method
//接收通知  同意添加好友后，刷新页面数据
- (void)refreshFrinedsListNot {
    [self.dataArr removeAllObjects];
    [self initData];
}
#pragma mark - getter and setter
- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myTableView.rowHeight = 70;
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
    }
    return _myTableView;
}
- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}
- (SearchView *)searchView {
    if (!_searchView) {
        _searchView = [[SearchView alloc] init];
        _searchView.backgroundColor = [UIColor whiteColor];
        _searchView.searchBar.placeholder = @"搜索";
    }
    return _searchView;
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end

